﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using TestUp.Models;
using TestUp.Models.Helpers;
using TestUp.Models.ShortEntities;
using TestUp.Models.TestWorkers;
using TestUp.Models.UserModels;

namespace TestUp.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult PendingTests()
        {
            UserInfo currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);
            if (currentUser != null)
            {
                var shortPendingTest = new List<ShortPendingTest>();
                currentUser.Group.PendingTests.ToList().ForEach(x => shortPendingTest.Add(x.ConvertToShortEntity()));
                return View(new PendingTestsModel
                    {
                        UserName = currentUser.UserName,
                        Permission = currentUser.Permission,
                        PendingTests = shortPendingTest
                    });
            }
            return RedirectToAction("Index", "Home");
        }

        public ActionResult PassingResults()
        {
            UserInfo currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);

            return View(new PassingResultsModel
                            {
                                UserName = currentUser.UserName,
                                Permission = currentUser.Permission,
                                PassingResults = currentUser.PassingResults.ToList()
                            });
        }

        public ActionResult OpenPendingTest(int? id)
        {
            if (id.HasValue)
            {
                var pendingTest = DataHelpers.PendingTestsHelper.GetPendingTest(id.Value);
                var test = DataHelpers.TestHelper.GetTest(pendingTest.TestId);
                string testPath = DataHelpers.TestHelper.GetTestPath(test);
                if (System.IO.File.Exists(testPath))
                {
                    var testData = XmlTestParser.ParseXmlFile(XDocument.Load(testPath));
                    //testData.Questions.ForEach(x=>x.PossibleAnswers.ForEach(ans=>ans.Item2 = false));
                    var model = new OpenTestModel
                        {
                            PendingTest = pendingTest,
                            TestData = testData
                        };
                    return View(model);
                }
            }
            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult SubmitTest(OpenTestModel model)
        {
            var currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);
            var testData = DataHelpers.TestHelper.LoadTest(model.PendingTest.TestId);
            if (currentUser != null && testData != null)
            {
                int resultCounter = 0;
                for (int i = 0; i < testData.Questions.Count; i++)
                {
                    /*if (testData.Questions[i].Check(model.TestData.Questions[i]))
                        resultCounter++;*/
                }
                DataHelpers.PassingResultHelper.CreatePassingResult(currentUser.UserId, model.PendingTest.TestId, resultCounter);
                DataHelpers.PendingTestsHelper.DeletePendingTest(model.PendingTest.PendingTestId);
                return RedirectToAction("PassingResults", "User");
            }
            return RedirectToAction("Index", "Home");
        }
    }
}
